#ifndef MASTERTHREAD_H
#define MASTERTHREAD_H

#include <QMutex>
#include <QThread>
#include <QWaitCondition>
#include <QSerialPort>
#include <QTime>

namespace Moolink {
    class MasterThread : public QThread
    {
        Q_OBJECT

    public:
        explicit MasterThread(QObject *parent = nullptr);
        ~MasterThread();

        void transaction(const int devIndex,const QString &devName,const QString &portName,const QSerialPort::BaudRate baudrate, QSerialPort::DataBits databits, QSerialPort::Parity parity, QSerialPort::StopBits stopbits, int waitTimeout, const QByteArray &request);
        //void threadQuit();

    signals:
        void response2(const int devIndex,const QString &devName,const QByteArray &res);
        void error(const int devIndex,const QString &devName,const QString &s);
        //void timeout(const int devIndex,const QString &devName,const QString &s);

    private:
        void run() override;
        int m_devIndex;
        QString m_devName;
        QString m_portName;
        QSerialPort::BaudRate m_baudrate;
        QSerialPort::DataBits m_databits;
        QSerialPort::Parity m_parity;
        QSerialPort::StopBits m_stopbits;
    //    QQueue<QString> m_request;
        QByteArray m_request;
        int m_waitTimeout = 0;
        QMutex m_mutex;
        QWaitCondition m_cond;
        bool m_quit = false;
    };

}
#endif // MASTERTHREAD_H
